﻿using System;
using FluentPS.Services;

namespace FluentPS.Impersonation
{
    public class Impersonation: IImpersonation
    {
        private IPsiContextService _psiContextService;

        public Impersonation(IPsiContextService psiContextService)
        {
            _psiContextService = psiContextService;
        }

        public PsiImpersonationContext CreateContext(string userNTAccount, Guid resourceGuid, Guid siteId)
        {
            return CreateContext(true, Guid.NewGuid(), "1033", userNTAccount, resourceGuid, siteId);
        }

        public PsiImpersonationContext CreateContext(bool isWindowsAccount, Guid trackingGuid, string lcid, string userNTAccount, Guid resourceGuid, Guid siteId)
        {
            var context = new PsiImpersonationContext(this)
                       {
                           IsWindowsAccount = isWindowsAccount,
                           TrackingGuid = trackingGuid,
                           Lcid = lcid,
                           UserNTAccount = userNTAccount,
                           ResourceGuid = resourceGuid,
                           SiteId = siteId
                       };

            _psiContextService.SetImpersonationContext(context);

            return context;
        }

        public void RemoveContext(PsiImpersonationContext context)
        {
            _psiContextService.RemoveImpersonationContext(context);
        }
    }
}
